var express = require('express');
const { get, addMovie,deleteMovie,alertMovie,findOne,update,delImgs,delposter,select} = require("../api/movies")
var router = express.Router();// 路由
const_=require("lodash");

// // 引入添加电影数据接口
// const moviesModel = require('../db/model/movies');



// 获取电影数据  get类型
router.get('/', async function (req, res, next) {
    // 解构当前页和每页显示条数
    const { curPage, eachPage } = req.query;
    const data = await get({ curPage, eachPage });
    res.send(data);

});

// 添加电影数据 
router.post('/add-movie', async function (req, res, next) {
    // 解构当前页和每页显示条数 create创建添加内容
    const { name, ename, type, area, time, upDate, score, count, intro, isClassic, director, actor } = req.body;
    const data = await addMovie({ name, ename, type, area, time, upDate, score, count, intro, isClassic, director, actor })
    // console.log(data);
    res.send(data);
    // console.log(req.body);
});

// 通过id查找电影数据  :表示动态
router.get('/findOne/:_id', async function (req, res, next) {
    // params去做路径解析
    const { _id } = req.params;
    const data = await findOne(_id);
    res.send(data)
});
// 获取所有信息
router.get('/select', async function (req, res, next) {
    const slectInfo=req.query
    const data = await select(slectInfo);
    res.send(data)
});

// 更新图片数据
router.patch('/:_id', async function (req, res, next) {
    // 从url拿参数
    const { _id } = req.params;
    const data = req.body;
    // 传值更新
    await update({ _id }, data);
    // 返回更新的整条数据
    const result = await findOne({ _id });
    res.send(result)
});

// 删除照片路由
router.delete('/:_id/imgs/:url', async function (req, res, next) {
    // 从url拿参数
    const { _id, url } = req.params;
    await  delImgs({ _id, url });
    //获取到修改保存后的最新数据并返回
    const data = await findOne({ _id });
    res.send(data);

});

// 删除照片路由
router.delete('/:_id/poster/:url', async function (req, res, next) {
    // 从url拿参数
    const { _id, url } = req.params;
    await  delposter({ _id, url });
    //获取到修改保存后的最新数据并返回
    const data = await findOne({ _id });
    res.send(data);


});



// 删除电影
router.delete("/:_id", async function (req, res, next) {
    const { _id } = req.body;
    await deleteMovie({ _id });
    res.send({ status: true });
});

//修改电影
router.put("/:_id", async function (req, res, next) {
    const newMovie= req.body;
    await alertMovie(newMovie);
    res.send({ status: true });
})
module.exports = router;
